package com.platform.wechat.service.impl;

import com.platform.wechat.common.JsonBean;
import com.platform.wechat.entity.Doctor;
import com.platform.wechat.entity.Patient;
import com.platform.wechat.entity.Platform;
import com.platform.wechat.entity.Registration;
import com.platform.wechat.mapper.DoctorMapper;
import com.platform.wechat.mapper.PatientMapper;
import com.platform.wechat.mapper.PlatformMapper;
import com.platform.wechat.mapper.RegistrationMapper;
import com.platform.wechat.service.RegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author gpx
 * @company XXXX公司
 * @create 2021-07-28 15:01
 */
@Service
public class RegistrationServiceImpl implements RegistrationService {

    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private RegistrationMapper registrationMapper;

    @Autowired
    private PlatformMapper platformMapper;

    @Autowired
    private PatientMapper patientMapper;

    /**
    * @Description:  时间范围查询（今天+最近七天）
    * @Param: [status, hospitalId]
    * @return: com.platform.wechat.common.JsonBean
    * @Author: guopeixin
    * @Date: 2021/7/28
    * @Time: 15:09
    */
    @Override
    public JsonBean selectDoctorByTime(Integer officesId, Integer hospitalId) {
        List<Doctor> list = doctorMapper.selectDoctorByTime(officesId, hospitalId);
        System.err.println(list.toString()+"--------list--------");
        for (Doctor doctor : list) {
            Date visitTime = doctor.getVisitTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
            String format = simpleDateFormat.format(visitTime);
            try {
                Date date = simpleDateFormat.parse(format);
                doctor.setVisitTime(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return new JsonBean(0,"ok",list);
    }

    /** 
    * @Description: 更具医院，科室，医生职称，姓名查询
    * @Param: [hospitalName, officesName, doctorTitle, doctorName] 
    * @return: com.platform.wechat.common.JsonBean 
    * @Author: guopeixin
    * @Date: 2021/7/28 
    * @Time: 20:24
    */
    @Override
    public JsonBean selectDoctor(String hospitalName, String officesName, String doctorTitle, String doctorName) {
        List<Doctor> list = doctorMapper.selectDoctor(hospitalName, officesName, doctorTitle, doctorName);
        System.err.println(list.toString()+"5656");
        return new JsonBean(0,"ok",list);
    }


    /**
     * 根据就诊人查订单
     * @param patientName
     * @return
     */
    @Override
    public JsonBean select(String patientName) {
        Patient patient = patientMapper.selectByPrimaryKeys(patientName);
        List<Registration> registrations = registrationMapper.selectPrimary(patient.getPatientId());
        JsonBean ok = new JsonBean(0, "ok", registrations);
        if (registrations==null){
            ok.setData(null);
        }
        return ok;
    }

    /**
     * 根据电话查就诊人id
     * @param platformPhone
     * @return
     */
    @Override
    public JsonBean selectName(String platformPhone) {
        Platform platform = platformMapper.selectPrismaryBys(platformPhone);
        List<Patient> patients = patientMapper.selectPrimary(platform.getPlatformId());
        return new JsonBean(0,"ok",patients);
    }

    /**
     * 查询详细信息
     * @param registrationId
     * @return
     */
    @Override
    public JsonBean selects(Integer registrationId) {
        Registration registration = registrationMapper.selectByPrimaryKey(registrationId);
        return new JsonBean(0,"ok",registration);
    }


    /** 
    * @Description: 添加Registration 挂号记录
    * @Param: [registration] 
    * @return: com.platform.wechat.common.JsonBean 
    * @Author: guopeixin
    * @Date: 2021/7/28 
    * @Time: 22:03
    */
    @Override
    public JsonBean insertRegistrationRecord(Registration registration) {
        JsonBean jsonBean = new JsonBean(-1,"失败",null);
        registration.setState(0);
        int i = registrationMapper.insertSelective(registration);
        if (i>0){
            jsonBean.setMsg("ok");
            jsonBean.setCode(0);
        }
        return jsonBean;
    }


}
